//
//  ReadSecondViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "ReadSecondViewController.h"
#import "AsyncPOSTParser.h"
#import "FileNameList.h"
#import "AFHTTPRequestOperationManager.h"
#import "Path.h"
#import "ReadModel.h"
#import "ReadDB.h"
#import "ReadDBDataBase.h"
#import "UMSocial.h"

@interface ReadSecondViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) NSDictionary *dicOfdata;
@property (nonatomic, retain) NSDictionary *userInfo;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UIView *underView;
@property (nonatomic, retain) UIButton *collectButton;
@property (nonatomic, retain) ReadModel *model;
@property (nonatomic, retain) UIButton *backButton;
@property (nonatomic, retain) NSString *userID;

// 分享
@property (nonatomic, retain) UIButton *shareButton;

@property (nonatomic, retain) UIWebView *web;

@property (nonatomic, retain) UIView *upView;

@end

@implementation ReadSecondViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dicOfdata = [NSDictionary dictionary];
        self.userInfo = [NSDictionary dictionary];
    }
    return self;
}

- (void)dealloc
{
    [_nameLabel release];
    [_titleLabel release];
    [_userInfo release];
    [_upView release];
    [_userID release];
    [_web release];
    [_shareButton release];
    [_backButton release];
    [_model release];
    [_collectButton release];
    [_dicOfdata release];
    [_underView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor cyanColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUnder];
}

- (void)receivewithModel:(ReadModel *)model
{
    self.userID = model.id;
    self.model = model;
    [self handleParser];
    
}

- (void)createUnder
{
    
    // 创建底边
    self.underView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 64 * heightFit, self.view.frame.size.width, 64 * heightFit)];
    self.underView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.underView];
    
    
    // 返回按钮
    self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(15 * widthFit, 10 * heightFit, 40 * widthFit, 40 *widthFit)];
    [self.backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"back.png"]] forState:UIControlStateNormal];
    self.backButton.layer.cornerRadius = self.backButton.frame.size.width / 2;
    [self.backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    self.backButton.layer.masksToBounds = YES;
    [self.underView addSubview:self.backButton];
    [_backButton release];
    
    // 分享按钮
    self.shareButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 55 * widthFit, 10 * heightFit, 40 * widthFit, 40 * widthFit)];
    [self.shareButton setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"share.png"]] forState:UIControlStateNormal];
    self.shareButton.layer.cornerRadius = self.shareButton.frame.size.height / 2;
    self.shareButton.layer.masksToBounds = YES;
    [self.shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [self.underView addSubview:self.shareButton];
    [_shareButton release];
    
    // 收藏按钮
    self.collectButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 20 * widthFit, 10 * heightFit, 40 , 44 )];
    [self.collectButton addTarget:self action:@selector(collected:) forControlEvents:UIControlEventTouchUpInside];
    ReadModel *model = [ReadDBDataBase selectWithModelID:self.model.id];
    
    if (model) {
        [self.collectButton setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"collected.png"]] forState:UIControlStateNormal];
    }
    else {
        [self.collectButton setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"collect.png"]] forState:UIControlStateNormal];
    }
    [self.underView addSubview:self.collectButton];
    [_collectButton release];
    [_underView release];

}

- (void)handleParser
{
    
    NSDictionary *dic = @{@"contentid" : self.model.id, @"client" : @"2"};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:@"pianke_session=eyJpdiI6IjFmVWVxQVJOSjZTSlhqTTFrRFg2cWc9PSIsInZhbHVlIjoiNXAxdnRTSnFWRUZEQVI5MFZMMGkyVjJRZUpvNlIyRWw3T0lGbVdsOGpjTWRmTkNHNHhyWVRBNVpZb3lycWk4K3FBM1NPd0owMmh2Z1dTNjU3WjJxNHc9PSIsIm1hYyI6ImU1NTgwNzdjMjA3ZTQ2MjAwOGExMzU0OTA2Yzg4MTM5ODM0N2M0ZTQ5YmFlZDFlZTYwNDdlNWJjYTZjZGY1ODMifQ%3D%3D" forHTTPHeaderField:@"Cookie"];
    [AsyncPOSTParser asyncPOSTParserWithURLString:LY_ReadOfSecond fileName:FILE_ReadOfSecond pamarters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dicOfdata = [dic objectForKey:@"data"];
        
        self.userInfo = [self.dicOfdata objectForKey:@"userinfo"];
        
        //[self createUnder];
        [self createSubView];
    }];
}

- (void)createSubView
{
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.web.scrollView.delegate = self;
    NSDictionary *dic = [self.dicOfdata objectForKey:@"shareinfo"];
    
    NSURL *url = [NSURL URLWithString:[dic objectForKey:@"url"]];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.web  loadRequest:request];
    
    [self.view addSubview:self.web];
    
    [_web release];

    // 创建上边
    self.upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160 * heightFit)];
    self.upView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.upView];
    [_upView release];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120 * heightFit / 2 - 20 * heightFit, self.view.frame.size.width, 60 * heightFit)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    self.titleLabel.text = [[self.dicOfdata objectForKey:@"shareinfo"] objectForKey:@"title"];
    self.titleLabel.backgroundColor = [UIColor whiteColor];
    [self.upView addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2, 100 * heightFit, self.view.frame.size.width / 2, 30 * heightFit)];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    NSString *name = [NSString stringWithFormat:@"by:%@", [self.userInfo objectForKey:@"uname"]];
    self.nameLabel.textColor = [UIColor blueColor];
    self.nameLabel.text = name;
    self.nameLabel.backgroundColor = [UIColor whiteColor];
    [self.upView addSubview:self.nameLabel];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < 0) {
        self.upView.frame = CGRectMake(0, 0, self.view.frame.size.width, -scrollView.contentOffset.y + 160 * heightFit);
        self.titleLabel.center = CGPointMake(self.upView.center.x, self.upView.center.y - 10 * heightFit);
        self.nameLabel.center = CGPointMake(self.nameLabel.center.x, self.upView.center.y + 35 * heightFit);
    }

}


// 点击分享
- (void)share:(UIButton *)button
{
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [Path pathWithImageView:imageView URLString:self.model.coverimg];
    UIImage *image = imageView.image;
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55df0fa2e0f55aa742001bc4" shareText:@"来一句吧" shareImage:image shareToSnsNames:@[UMShareToRenren, UMShareToDouban, UMShareToSina, UMShareToTencent] delegate:nil];
}




// 点击返回
- (void)backButton:(UIButton *)button
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


// 收藏点击事件
- (void)collected:(UIButton *)button
{
    
    BOOL status = [ReadDBDataBase insertWithmodel:self.model];
    if (status == YES) {
        [self.collectButton setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"collected.png"]] forState:UIControlStateNormal];
    }
    else {
        [self.collectButton setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"collect.png"]] forState:UIControlStateNormal];
        [ReadDBDataBase deleteWith:self.model];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
